#include "../Header Files/CAssetManager_AL.h"	
#include "../Header Files/Direct3D_AL.h"

CAssetManager_AL::CAssetManager_AL()
{
	LoadEffects();
	CreateCube();
}
CAssetManager_AL::~CAssetManager_AL()
{

}

CAssetManager_AL* CAssetManager_AL::GetInstance()
{
	static CAssetManager_AL instance;
	return &instance;
}

void CAssetManager_AL::LoadEffects()
{
	ID3DXBuffer	*errors(NULL);
	ID3DXEffect *effect = NULL;
	D3DXCreateEffectFromFile(Direct3D_AL::GetInstance()->m_pD3DDev, "..\\Placeholder\\Assets\\Shaders\\Cube.fx", 0, 0, D3DXSHADER_DEBUG, 0, &effect, &errors);
	if (errors)
	{
		MessageBox(0, (char*)errors->GetBufferPointer(), 0, 0);
	}

	m_vEffects.push_back(effect);

	effect = NULL;
	D3DXCreateEffectFromFile(Direct3D_AL::GetInstance()->m_pD3DDev, "..\\Placeholder\\ASsets\\Shaders\\Ground.fx", 0, 0, D3DXSHADER_DEBUG, 0, &effect, &errors);
	if (errors)
	{
		MessageBox(0, (char*)errors->GetBufferPointer(), 0, 0);
	}

	m_vEffects.push_back(effect);
}

//-----------------------------------------------------------------
// ADDING ITEMS TO THE LIST
int CAssetManager_AL::AddTexture(IDirect3DTexture9* texture)
{
	m_vTextures.push_back(texture);
	return m_vTextures.size() - 1;
}
int CAssetManager_AL::AddVertBuff(IDirect3DVertexBuffer9* buff)
{
	m_vVertBuffers.push_back(buff);
	return m_vVertBuffers.size() - 1;
}
int CAssetManager_AL::AddVertDecl(IDirect3DVertexDeclaration9* decl)
{
	m_vVertDecls.push_back(decl);
	return m_vVertDecls.size() - 1;
}
int CAssetManager_AL::AddIndexBuff(IDirect3DIndexBuffer9* indexbuff)
{
	m_vIndexBuffers.push_back(indexbuff);
	return m_vIndexBuffers.size() - 1;
}
int CAssetManager_AL::AddEffect(ID3DXEffect* effect)
{
	m_vEffects.push_back(effect);
	return m_vEffects.size() - 1;
}
//*****************************************************************

int CAssetManager_AL::CreateTexture(string _szTexturePath)
{
	IDirect3DTexture9 *tempTexture;
	string t = "..\\Placeholder\\Assets\\Textures\\";
	t += _szTexturePath;
	D3DXCreateTextureFromFileA(Direct3D_AL::GetInstance()->m_pD3DDev, t.c_str(), &tempTexture);
	m_vTextures.push_back(tempTexture);
	return m_vTextures.size() - 1;
}
IDirect3DTexture9* CAssetManager_AL::GetTexture(int _nID)
{
	if (m_vTextures.size() > (unsigned int)_nID)
		return m_vTextures[_nID];
	return NULL;
}


void CAssetManager_AL::CreateCube()
{
	IDirect3DVertexBuffer9* vertBuff = NULL;

	Verts_PosCol Cube[8];
	Cube[0].Position = D3DXVECTOR3(-1, 1, -1);
	Cube[1].Position = D3DXVECTOR3(1, 1, -1);
	Cube[2].Position = D3DXVECTOR3(1, -1, -1);
	Cube[3].Position = D3DXVECTOR3(-1, -1, -1);

	Cube[4].Position = D3DXVECTOR3(-1, 1, 1);
	Cube[5].Position = D3DXVECTOR3(1, 1, 1);
	Cube[6].Position = D3DXVECTOR3(1, -1, 1);
	Cube[7].Position = D3DXVECTOR3(-1, -1, 1);

	Direct3D_AL::GetInstance()->m_pD3DDev->CreateVertexBuffer(8 * sizeof(Verts_PosCol), 0, 0, D3DPOOL_DEFAULT, &vertBuff, 0);
	void* buffer;
	vertBuff->Lock(0,
		8 * sizeof(Verts_PosCol),
		&buffer,
		D3DLOCK_DISCARD);
	memcpy(buffer, Cube, 8 * sizeof(Verts_PosCol));
	vertBuff->Unlock();

	m_vVertBuffers.push_back(vertBuff);

	WORD iList[36];
	//triangle1
	iList[0] = 0;
	iList[1] = 1;
	iList[2] = 2;
	//triangle2
	iList[3] = 2;
	iList[4] = 3;
	iList[5] = 0;
	//triangle3
	iList[6] = 5;
	iList[7] = 4;
	iList[8] = 7;
	//triangle4
	iList[9] = 7;
	iList[10] = 6;
	iList[11] = 5;
	//triangle5
	iList[12] = 4;
	iList[13] = 0;
	iList[14] = 3;
	//triangle6
	iList[15] = 3;
	iList[16] = 7;
	iList[17] = 4;
	//triangle7
	iList[18] = 1;
	iList[19] = 5;
	iList[20] = 6;
	//triangle8
	iList[21] = 6;
	iList[22] = 2;
	iList[23] = 1;
	//triangle9
	iList[24] = 4;
	iList[25] = 5;
	iList[26] = 1;
	//triangle10
	iList[27] = 1;
	iList[28] = 0;
	iList[29] = 4;
	//triangle11
	iList[30] = 3;
	iList[31] = 2;
	iList[32] = 6;
	//triangle12
	iList[33] = 6;
	iList[34] = 7;
	iList[35] = 3;

	IDirect3DIndexBuffer9* indexbuff = NULL;
	Direct3D_AL::GetInstance()->m_pD3DDev->CreateIndexBuffer(36 * sizeof(WORD), 0, D3DFMT_INDEX16, D3DPOOL_DEFAULT, &indexbuff, 0);
	indexbuff->Lock(0,
		36 * sizeof(WORD),
		&buffer,
		0);
	memcpy(buffer, iList, 36 * sizeof(WORD));
	indexbuff->Unlock();

	m_vIndexBuffers.push_back(indexbuff);

	IDirect3DVertexDeclaration9* cubeDecl = NULL;

	D3DVERTEXELEMENT9 decl[] = 
	{
		{ 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
		{ 0, 12, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0 },
		D3DDECL_END()
	};

	Direct3D_AL::GetInstance()->m_pD3DDev->CreateVertexDeclaration(decl, &cubeDecl);

	m_vVertDecls.push_back(cubeDecl);
}